<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${daoUrl}.${entityName}Mapper">

	<resultMap id="BaseResultMap" type="${entityUrl}.${entityName}">
		<!-- todo id标签放在第一位-->
	<#list cis as ci>
	    <#if ci.property == keyProperty>
	    <id column="${ci.column}" jdbcType = "${ci.jdbcTypeUp}" property="${ci.property}" />
	    <#else>
	    <result column="${ci.column}" jdbcType = "${ci.jdbcTypeUp}" property="${ci.property}" />
	    </#if>
	</#list>
	</resultMap>

	<resultMap id="BaseResultVoMap" type="${voUrl}.${entityName}Vo" extends="BaseResultMap">
	</resultMap>

	<sql id="Base_Column_List">
		${agile}
	</sql>

	<sql id="Table_Name">
		<#noparse>${custom}</#noparse>.${table}
	</sql>

	<select id="queryPageByParam" resultMap="BaseResultVoMap">
		SELECT <include refid="Base_Column_List"/>
		<#noparse><!-- todo 字典类型，调用函数，放在 Str字段中，如 F_FIND_CODIFIER_SNAME('码表code',需要码表的字段, #{queryParam.lang}) AS DATA_STATUS_Str--></#noparse>
		FROM <include refid="Table_Name" /> a
		<where>
			<if test="queryParam.${keyProperty} != null and queryParam.${keyProperty} != ''">
				a.${keyColumn} = <#noparse>#{</#noparse>queryParam.${keyProperty}<#noparse>}</#noparse>
			</if>
			<#noparse>
			<!-- 要改为相应模糊查找字段 todo -->
            <if test="queryParam.field != null and queryParam.field != ''">
                AND a.field like CONCAT('%',#{queryParam.field},'%')
            </if>
            </#noparse>
		</where>
	</select>

	<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultVoMap">
		select
		<include refid="Base_Column_List" />
		from <include refid="Table_Name" />
		where ${keyColumn} = <#noparse>#{primaryKey}</#noparse>
	</select>

    <select id="selectAllData" resultMap="BaseResultVoMap">
        select
        <include refid="Base_Column_List" />
        from <include refid="Table_Name" />
    </select>

	<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
		<!--@bo.generated-->
		delete from <include refid="Table_Name" />
		where ${keyColumn} = <#noparse>#{primaryKey}</#noparse>
	</delete>


	<insert id="insert" parameterType="${entityUrl}.${entityName}">
		insert into <include refid="Table_Name" />
		(${agile})
		values (${allFieldsParam})
	</insert>

    <insert id="batchInsert" parameterType="java.util.Map" keyProperty="${keyProperty}" useGeneratedKeys="true">
        insert into <include refid="Table_Name" />
        (${agile})
        values
        <foreach collection="map.list" item="record" separator=",">
            (${allFieldsParam})
        </foreach>
    </insert>


	<update id="updateByPrimaryKey" parameterType="${entityUrl}.${entityName}">
		update <include refid="Table_Name" />
		<set>
		<!-- todo 删除 createTime  和 createUser-->
		<#list cis as ci>
		<#if ci.jdbcTypeUp == 'VARCHAR'>
		    <#if ci.property != keyProperty>
			<if test="record.${ci.property}  != null and  record.${ci.property}!= ''">
				${ci.column} = <#noparse>#{</#noparse>record.${ci.property}<#noparse>}</#noparse>,
			</if>
            </#if>
		<#else>
		    <#if ci.property != keyProperty>
			${ci.column} = <#noparse>#{</#noparse>record.${ci.property}<#noparse>}</#noparse>,
            </#if>
		</#if>
		</#list>
		</set>
		where ${keyColumn} = <#noparse>#{</#noparse>record.${keyProperty}<#noparse>}</#noparse>
	</update>
	
</mapper>